package com.moon.robot.Utils;

import com.baidu.aip.contentcensor.EImgType;
import com.baidu.aip.ocr.AipOcr;


import com.moon.robot.RobotApplication;
import com.moon.robot.pojo.Jianhuang;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Badiushibie extends JSONObject {


    public static List<Jianhuang> simple(String imgurl) {


        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
           /* client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
            client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理*/

        // 调用接口
        JSONObject res = RobotApplication.Jianhuangclient.imageCensorComb(imgurl, EImgType.URL, Arrays.asList("antiporn"), null);
        String s = res.toString(2);
        List<Jianhuang> list1=new ArrayList<>();
        String[] split = s.split("\n");
        for (int i = 0; i < split.length; i++) {
            String s1 = split[i];
            List<String> list = Utils.getMatcher(s1, " \"probability\": (.*),");
            if (list!=null&&list.get(1)!=null){
                String s2 = split[i+1];
                List<String> list3 = Utils.getMatcher(s2, " \"class_name\": (.*)");
                String s3="";
                if (list3!=null&&list3.get(1)!=null){
                    s3 = list3.get(1).replaceAll("\"", "");
                }
                Jianhuang jianhuang = new Jianhuang();
                jianhuang.setProbability(list.get(1));
                jianhuang.setClass_name(s3);
                list1.add(jianhuang);
            }
        }
        return list1;
    }
    public static String OCR(String imgurl,String language_type) {

        //ocr
        String APP_ID2 = "17766026";
        final String API_KEY2 = "GvTGnYjPxsjCvWMrXjWlSarQ";
        final String SECRET_KEY2 = "xpucZuvtFGqrTBEOOUBVq0QAvHGAbKKv";
        AipOcr aipOcr = new AipOcr(APP_ID2, API_KEY2, SECRET_KEY2);
        // 可选：设置网络连接参数
        aipOcr.setConnectionTimeoutInMillis(2000);
        aipOcr.setSocketTimeoutInMillis(60000);
        String a="";
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", language_type);
        options.put("detect_language", "true");
        JSONObject res = aipOcr.basicGeneralUrl(imgurl,options);
        JSONArray words_result = res.getJSONArray("words_result");




        for(int i=0;i<words_result.length();i++) {
            a=a+words_result.getJSONObject(i).get("words")+"\n";

        }


        return a;
    }

}
